package io.github.i678.namedutils.api;

import io.github.i678.namedutils.api.utils.ConfigurationUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

/**
 * Staffchat tools.
 * @author TheNameMan
 */
public class Staffchat {
      
    static boolean enabled;
    static List<String> toggledStaffchat = new ArrayList<>();

    public static void setEnabled(boolean enable) {
        enabled = enable;
    }
    
    public static boolean isEnabled() {
        return enabled;
    }

    public static void chat(String message, Player player) {
        if(!enabled) throw new UnsupportedOperationException("Staff chat is flagged DISABLED in the configuration file.");
        String displayName = player.getDisplayName();
        String prefix = Bukkit.getPluginManager().getPlugin("StarterTools").getConfig().getString("staffchat.prefix");
        String rawMsg = ConfigurationUtils.getString("staffchat.message");
        rawMsg.replaceAll("{USER}", displayName);
        rawMsg.replaceAll("{PREFIX}", prefix);
        rawMsg.replaceAll("{MESSAGE}", message);
        String permission = Bukkit.getPluginManager().getPlugin("StarterTools").getConfig().getString("staffchat.permission");
        internalStaffChat(prefix, rawMsg, permission, null, ConfigurationUtils.getStringList("staffchat.ignoreStaffChat"));
    }
    
    private static void internalStaffChat(String prefix, String message, String permission, ChatColor[] seperators, List<String> ignore) {
        for(Player p : Bukkit.getOnlinePlayers()) {
            if(!p.hasPermission(permission)) continue;
            if(ignore.contains(p.getName())) continue;
            p.sendMessage(message);
        }
    }

    public static void toggleStaffchat(Player player, boolean toggle) {
        if(toggle) {
            try {
                toggledStaffchat.add(player.getName());
            } catch(Exception ignore) {}
        } else {
            try {
                toggledStaffchat.remove(player.getName());
            } catch(Exception ignore) {}
        }
    }

    public static boolean staffchatToggled(String player) {
        return toggledStaffchat.contains(player);
    }
    
}